﻿using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WPF_UI_Lightningchart.Events;
using WPF_UI_Lightningchart.Services;
using WPF_UI_Lightningchart.UserControls;
using WPF_UI_Lightningchart.ViewModels;

namespace WPF_UI_Lightningchart.Views
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly LayoutManager _layoutManager = new LayoutManager();
        private readonly IEventAggregator _eventAggregator;
        public MainWindow(IEventAggregator eventAggregator)
        {
            InitializeComponent();
            _eventAggregator = eventAggregator;
            var gridLayoutUserControl = new GridLayoutUserControl(eventAggregator);
            gridLayoutUserControl.Name = "LayoutUserControl";
            GridLayoutUserControl.Children.Add(gridLayoutUserControl);
        }



        private void OnCreateLayoutClick(object sender, RoutedEventArgs e)
        {

            // 获取当前的 GridLayoutUserControl 实例
            var gridLayoutUserControl = GridLayoutUserControl.Children
                .OfType<GridLayoutUserControl>() // 获取 GridLayoutUserControl 类型的子控件
                .FirstOrDefault();

            // 检查是否成功获取到 GridLayoutUserControl
            if (gridLayoutUserControl != null)
            {
                // 告知 GridLayoutUserControl 加载选定的布局
                gridLayoutUserControl.CreateNewLayout();
            }
            else
            {
                Console.WriteLine("未能找到 GridLayoutUserControl 实例。");
            }            
        }

        private void OnLayoutSelected(object sender, RoutedEventArgs e)
        {

            // 获取被点击的 Button 控件
            var button = sender as Button;

            // 获取 CommandParameter，这里假设是布局名称
            var selectedLayoutName = button?.CommandParameter as string;

            if (string.IsNullOrEmpty(selectedLayoutName)) return;


            // 获取当前的 GridLayoutUserControl 实例
            var gridLayoutUserControl = GridLayoutUserControl.Children
                .OfType<GridLayoutUserControl>() // 获取 GridLayoutUserControl 类型的子控件
                .FirstOrDefault();



            // 检查是否成功获取到 GridLayoutUserControl
            if (gridLayoutUserControl != null)
            {
                // 添加确认对话框
                var result = MessageBox.Show($"确定要加载布局 '{selectedLayoutName}' 吗？", "确认加载布局",
                    MessageBoxButton.YesNo, MessageBoxImage.Question);

                if (result == MessageBoxResult.Yes)
                {
                    // 告知 GridLayoutUserControl 加载选定的布局
                    gridLayoutUserControl.LoadLayout(selectedLayoutName);
                }
            }
            else
            {
                Console.WriteLine("未能找到 GridLayoutUserControl 实例。");
            }
        }

    }
}
